package it.uniroma2.art.owlart.utilities.transform;

import it.uniroma2.art.owlart.exceptions.ModelAccessException;
import it.uniroma2.art.owlart.exceptions.ModelUpdateException;
import it.uniroma2.art.owlart.model.ARTLiteral;
import it.uniroma2.art.owlart.model.ARTNode;
import it.uniroma2.art.owlart.model.ARTResource;
import it.uniroma2.art.owlart.model.ARTStatement;
import it.uniroma2.art.owlart.model.ARTURIResource;
import it.uniroma2.art.owlart.model.NodeFilters;
import it.uniroma2.art.owlart.models.SKOSModel;
import it.uniroma2.art.owlart.models.SKOSXLModel;
import it.uniroma2.art.owlart.navigation.ARTStatementIterator;
import it.uniroma2.art.owlart.vocabulary.SKOS;
import it.uniroma2.art.owlart.vocabulary.SKOSXL;

/**
 * 
 * 
 * @author Manuel Fiorelli
 * 
 */
public class SKOSXL2SKOSConverter {

	public void doConversion(SKOSXLModel sourceModel, SKOSModel targetModel) throws ModelAccessException,
			ModelUpdateException {
		ARTStatementIterator it = sourceModel.listStatements(NodeFilters.ANY, NodeFilters.ANY,
				NodeFilters.ANY, false, NodeFilters.MAINGRAPH);

		while (it.streamOpen()) {
			ARTStatement stmt = it.next();

			ARTResource subj = stmt.getSubject();
			ARTURIResource pred = stmt.getPredicate();
			ARTNode obj = stmt.getObject();

			if (sourceModel.isXLabel(subj)) {
				continue;
			}

			if (obj.isResource() && sourceModel.isXLabel(obj.asResource())) {
				ARTURIResource inferredPred = null;
				if (pred.equals(SKOSXL.Res.PREFLABEL)) {
					inferredPred = SKOS.Res.PREFLABEL;
				} else if (pred.equals(SKOSXL.Res.ALTLABEL)) {
					inferredPred = SKOS.Res.ALTLABEL;
				} else if (pred.equals(SKOSXL.Res.HIDDENLABEL)) {
					inferredPred = SKOS.Res.HIDDENLABEL;
				}

				ARTLiteral inferredObj = sourceModel.getLiteralForm(obj.asResource());

				System.err.println("@@" + inferredObj);
				if (inferredPred != null) {
					System.err.println("@@" + stmt.getSubject() + " " + inferredPred + " " + inferredObj);
					targetModel
							.addTriple(stmt.getSubject(), inferredPred, inferredObj, NodeFilters.MAINGRAPH);
				} else {
					continue;
				}
				continue;
			}
			
			targetModel.addStatement(stmt, NodeFilters.MAINGRAPH);
		}
		it.close();
	}
}
